//index.js
//获取应用实例
const app = getApp();
// var template = require('../header/header.js');

Page({
    data: {
        qrcode: '',
        userInfo: [],
        currentPageNum: 0,
        pageSize: 10,
        shareList:[],
    },
    onLoad: function () {
        
        let _this = this;
        
        this.getQrcode()

        _this.loaderShareList()

        this.setData({
            userInfo: app.globalData.userInfo
        })
    },
    
    onHide: function () {
    },

    // 加载分享列表
    loaderShareList(){
    
        let _this = this;
        let { currentPageNum, pageSize }  = this.data;

        return app.request({
            url: app.api.url.user.shareList,
            data: {
                page: currentPageNum,
                pageSize
            }
        }).then(res =>{

            if(res.status == 1){

                // 格式化时间
                res.data = res.data.map(item =>{
                    item.createTime = app.util.dateTime(item.createTime, 'Y-m-d')
                    return item;
                })
                
                // 拼接数据
                let shareList = _this.data.shareList.concat(res.data)

                _this.setData({
                    shareList
                })
            }         
            
        })
        
    },
    
    onShareAppMessage: function (ops) {
        if (ops.from === 'button') {
            // 来自页面内转发按钮
            console.log(ops.target)
        }
        
        return {
            title: this.data.userInfo.username+'邀请您加入网匠生活',
            imageUrl: this.data.qrcode,//图片地址
            path:'/pages/index/index?user_id='+this.data.userInfo.id,// 用户点击首先进入的当前页面
            success: function (res) {
                app.showToast('分享成功')
            },
            fail: function (res) {
                app.showToast('分享失败')
            }
            
        }

    },

    // 下拉刷新
    onReachBottom(){
        this.loaderShareList();
    },

    assistant: function() {
        var object = {
            url: app.api.url.assistant.service,
            data: {type: 2},
            success: function(res) {
                if(res.status == 1) {
                    app.navigateTo(app.page.wapUrl, 'url='+res.data.url);
                }
            }
        }
        app.request(object);
    },

    // 获取二维码
    getQrcode: function(){
        
        var _this = this;
        var object = {
            url: app.api.url.user.qrcode,
        }
        
        return app.request(object).then(res =>{

            if(res.status == 1){

                _this.setData({
                    qrcode: res.url
                })

            }
                
        });
    }
});
